Bash (Unix shell)

Bash

Screenshot of Bash and sh sessions demonstrating some features
Original author(s) Brian Fox
Initial release June 7, 1989; 22 years ago (1989-06-07)
Stable release 4.2  (February 13, 2011; 11 months ago (2011-02-13))[1] [±]
Development status Active
Written in C
Operating system Cross-platform
Platform GNU
Available in English, multilingual (gettext)
Type Unix shell
License GNU General Public License version 3+[2]
Website Bash GNU project home page

Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh).[3][4] Released in 1989,[5] it has been distributed widely as the shell for the GNU operating system and as the default shell on Linux, Mac OS X and Darwin. It has been ported to Microsoft Windows and distributed with Cygwin and MinGW, to DOS by the DJGPP project and to Novell NetWare.

Bash is a command processor, typically run in a text window, allowing the user to type commands which cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language were all copied from sh. Other features, e.g., history, were copied from csh and ksh. Bash is a POSIX shell but with a number of extensions.

The name itself is an acronym, a pun and descriptive. As an acronym, it stands for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.[6] As a pun, it expressed that objective in a phrase that sounds the same as born again, a term for spiritual rebirth.[7][8] The name is also descriptive of what it did, bashing together the features of sh, csh and ksh.[9]

Contents

History

Brian Fox began coding Bash on January 10, 1988[10] after Richard Stallman became dissatisfied with the lack of progress being made by a prior developer.[3] Stallman and the Free Software Foundation (FSF) considered a free shell that could run existing sh scripts so strategic to a completely free system built from BSD and GNU code that this was one of the few projects they funded themselves, with Fox undertaking the work as an employee of FSF.[3][11] Fox released Bash as a beta, version .99, on June 7, 1989[5] and remained the primary maintainer until sometime between mid-1992[12] and mid-1994,[13] when he was laid off from FSF[14] and his responsibility was transitioned to another early contributor, Chet Ramey.[15][16][17]

Features

The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification, with the exception of Bourne shell scripts stumbling into fringe syntax behavior interpreted differently in Bash or attempting to run a system command matching a newer Bash builtin, etc. Bash command syntax includes ideas drawn from the Korn shell (ksh) and the C shell (csh) such as command line editing, command history, the directory stack, the $RANDOM and $PPID variables, and POSIX command substitution syntax $(…). When used as an interactive command shell and pressing the tab key, Bash automatically uses command line completion to match partly typed program names, filenames and variable names. The Bash commmand-line completion system is very flexible and customizable, and is often packaged with functions that complete arguments and filenames for specific programs and tasks.

Bash's syntax has many extensions which the Bourne shell lacks. Bash can perform integer calculations without spawning external processes, unlike the Bourne shell. Bash uses the ((…)) command and the $((…)) variable syntax for this purpose. Bash syntax simplifies I/O redirection in ways that are not possible in the traditional Bourne shell. For example, Bash can redirect standard output (stdout) and standard error (stderr) at the same time using the &> operator. This is simpler to type than the Bourne shell equivalent 'command > file 2>&1'. Bash supports process substitution using the <(command) syntax, which substitutes the output of (or input to) a command where a filename is normally used.

When using the 'function' keyword, Bash function declarations are not compatible with Bourne/Korn/POSIX scripts (the Korn shell has the same problem when using 'function'), but Bash accepts the same function declaration syntax as the Bourne and Korn shells, and is POSIX conformant. Due to these and other differences, Bash shell scripts are rarely runnable under the Bourne or Korn shell interpreters unless deliberately written with that compatibility in mind, which is becoming less common as Linux becomes more widespread. But in POSIX mode,[18] Bash conformance with POSIX is nearly perfect.

Bash supports here documents just as the Bourne shell always has. However, since version 2.05b Bash can redirect standard input (stdin) from a "here string" using the <<< operator.

Bash 3.0 supports in-process regular expression matching using a syntax reminiscent of Perl.[19]

Bash 4.0 supports associative arrays[18] allowing faked support for multi-dimensional arrays, in a similar way to awk:

declare -A a         # declare an associative array 'a'
i=1; j=2             # initialize some indices
a[$i,$j]=5           # associate value "5" to key "$i,$j" (i.e. "1,2")
echo ${a[$i,$j]}     # print the stored value at key "$i,$j"

Brace expansion

Brace expansion, also called alternation, is a feature copied from the C shell that generates the set of alternative combinations. The generated results need not exist as files. The results of each expanded string are not sorted and left to right order is preserved:

echo a{p,c,d,b}e # ape ace ade abe
echo {a,b,c}{d,e,f} # ad ae af bd be bf cd ce cf

Brace expansions should not be used in portable shell scripts, because the Bourne shell will not produce the same output.

#!/bin/sh
 
# A traditional shell does not produce the same output
echo a{p,c,d,b}e # a{p,c,d,b}e

When brace expansion is combined with wildcards, the braces are expanded first, then the resulting wildcards are substituted normally. Hence, a listing of JPEG and PNG images in the current directory could be obtained with:

ls *.{jpg,jpeg,png}    # expands to *.jpg *.jpeg *.png - after which,
                       # the wildcards are processed

Startup scripts

When Bash starts, it executes the commands in a variety of different scripts.

When started as an interactive login shell:

  • Bash reads and executes the /etc/profile (if it exists).
  • After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and reads and executes the first one (that exists and is readable).

When a login shell exits:

  • Bash reads and executes ~/.bash_logout (if it exists).

When started as an interactive shell (but not a login shell):

  • Bash reads and executes ~/.bashrc (if it exists). This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.

Some versions of Unix have especially contorted system scripts for Bash which violate the documented script load order (by loading scripts too early or attempting to combine Bash startup with the startup scripts for other shells in various ways).

Portability

Shell scripts written with Bash-specific features (bashisms) will not function on a system using the Bourne shell or one of its replacements, unless Bash is also installed and the script begins with a "shebang line" of #!/bin/bash interpreter directive instead of #!/bin/sh.

Keyboard shortcuts

The following shortcuts work when using default (Emacs) key bindings. Vi-bindings can be enabled by running set -o vi.[20]

See also

References

  1. ^ Ramey, Chet (2011-02-16). "Bash-4.2 available for FTP". info-gnu mailing list. http://www.mail-archive.com/info-gnu@gnu.org/msg01139.html. Retrieved 2011-02-20. 
  2. ^ GNU Project. "README file". http://www.gnu.org/software/bash/. "Bash is free software, distributed under the terms of the [GNU] General Public License as published by the Free Software Foundation, version 3 of the License (or any later version)." 
  3. ^ a b c Richard Stallman (forwarded with comments by Chet Ramey) (February 10, 1988). "GNU + BSD = ?". comp.unix.questions. (Web link). "For a year and a half, the GNU shell was "just about done". The author made repeated promises to deliver what he had done, and never kept them. Finally I could no longer believe he would ever deliver anything. So Foundation staff member Brian Fox is now implementing an imitation of the Bourne shell.". Retrieved Mar 22, 2011. 
  4. ^ [|Hamilton, Naomi] (May 30, 2008), "The A-Z of Programming Languages: BASH/Bourne-Again Shell", Computerworld: 2, http://www.computerworld.com.au/article/222764/a-z_programming_languages_bash_bourne-again_shell/?pp=2&fp=16&fpid=1, retrieved Mar 21, 2011, "When Richard Stallman decided to create a full replacement for the then-encumbered Unix systems, he knew that he would eventually have to have replacements for all of the common utilities, especially the standard shell, and those replacements would have to have acceptable licensing." 
  5. ^ a b Brian Fox (forwarded by Leonard H. Tower Jr.) (June 8, 1989). "Bash is in beta release!". gnu.announce. (Web link). Retrieved Oct 28 2010. 
  6. ^ C Programming by Al Stevens, Dr. Dobb's Journal, July 1, 2001
  7. ^ Richard Stallman (Nov 12, 2010). "About the GNU Project". Free Software Foundation. http://www.gnu.org/gnu/thegnuproject.html. Retrieved Mar 13, 2011. "“Bourne Again Shell” is a play on the name “Bourne Shell”, which was the usual shell on Unix." 
  8. ^ Gattol, Markus (Mar 13, 2011), Bourne-again Shell, http://www.markus-gattol.name/ws/bash.html, retrieved Mar 13, 2011, "The name is a pun on the name of the Bourne shell (sh), an early and important Unix shell written by Stephen Bourne and distributed with Version 7 Unix circa 1978, and the concept of being "born again"." 
  9. ^ Ian Darwin (June 13, 1989). "at&t-free ksh (was: job control is a bug, not a feature)". comp.os.minix. (Web link). "Yup, the gnu project's Born Again Shell ("bash") is an attempt at bashing all the features of sh together with many of those from both csh and ksh.". Retrieved Mar 21, 2011. 
  10. ^ Brian Fox (August 29, 1996), shell.c, Free Software Foundation, http://ftp.gnu.org/gnu/bash/bash-1.14.7.tar.gz, "Birthdate: Sunday, January 10th, 1988. Initial author: Brian Fox" 
  11. ^ Richard Stallman (October 3, 2010). "About the GNU Project". Free Software Foundation. http://www.gnu.org/gnu/thegnuproject.html. Retrieved Mar 21, 2011. "Free Software Foundation employees have written and maintained a number of GNU software packages. Two notable ones are the C library and the shell. ... We funded development of these programs because the GNU Project was not just about tools or a development environment. Our goal was a complete operating system, and these programs were needed for that goal." 
  12. ^ len (g...@prep.ai.mit.edu) (April 20, 1993). "January 1993 GNU's Bulletin". gnu.announce. (Web link). Retrieved Oct 28 2010. 
  13. ^ Ramey, Chet (1994-08-01). "Bash - the GNU shell (Reflections and Lessons Learned)". Linux Journal. http://www.linuxjournal.com/article/2800#N0xa50890.0xb46380. Retrieved 2008-11-13. 
  14. ^ Chet Ramey (October 31, 2010), Dates in your Computerworld interview, http://www.scribd.com/doc/40556434/2010-10-31-Chet-Ramey-Early-Bash-Dates, retrieved Oct 31 2010 
  15. ^ Chet Ramey (June 12, 1989). "Bash 0.99 fixes & improvements". gnu.bash.bug. (Web link). Retrieved Nov 1 2010. 
  16. ^ Chet Ramey (July 24, 1989). "Some bash-1.02 fixes". gnu.bash.bug. (Web link). Retrieved Oct 30 2010. 
  17. ^ Brian Fox (March 2, 1990). "Availability of bash 1.05". gnu.bash.bug. (Web link). Retrieved Oct 30 2010. 
  18. ^ a b "6.11 Bash POSIX Mode", The GNU Bash Reference Manual, for Bash, Version 4.1, December 23, 2009, http://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html, retrieved Oct 26 2010 
  19. ^ The syntax matches that shown on the regex(7) man page.
  20. ^ http://www.hypexr.org/bash_tutorial.php#emacs

External links